//
//  OptionsScreen.m
//  Gekko
//
//  Created by jose on 02/06/13.
//  Copyright (c) 2013 Jose Luis Lopez. All rights reserved.
//

#import "OptionsScreen.h"

@interface OptionsScreen ()

@end

@implementation OptionsScreen

@synthesize buttonClear, buttonFeedback, buttonLogout, labelLearnMore, buttonRecomend, labelSlogan;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    [buttonLogout setTitle:NSLocalizedString(@"LOG OUT", nil) forState:UIControlStateNormal];
    [buttonLogout.titleLabel setFont:[UIFont fontWithName:kFontBold size:16]];
    
    [labelSlogan setText:NSLocalizedString(@"SLOGAN", nil)];
    [labelSlogan setFont:[UIFont fontWithName:kFontBold size:16]];

    [buttonFeedback setTitle:NSLocalizedString(@"FEEDBACK", nil) forState:UIControlStateNormal];
    [buttonFeedback.titleLabel setFont:[UIFont fontWithName:kFontBold size:16]];
    
    [buttonClear setTitle:NSLocalizedString(@"CLEAR DATA", nil) forState:UIControlStateNormal];
    [buttonClear.titleLabel setFont:[UIFont fontWithName:kFontBold size:16]];
    
    [buttonRecomend setTitle:NSLocalizedString(@"RECOMEND", nil) forState:UIControlStateNormal];
    [buttonRecomend.titleLabel setFont:[UIFont fontWithName:kFontBold size:16]];
    
    [labelLearnMore setText:NSLocalizedString(@"LEARN MORE", nil)];
    [labelLearnMore setFont:[UIFont fontWithName:kFontBold size:12]];
    [labelLearnMore sizeToFit];
    
    UIView *underline = [[UIView alloc] initWithFrame:CGRectMake(labelLearnMore.frame.origin.x,
                                                                 labelLearnMore.frame.origin.y + labelLearnMore.frame.size.height - 4, labelLearnMore.frame.size.width, 1)];
    [underline setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:underline];
    
    UIButton *buttonLearn = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonLearn setFrame:labelLearnMore.frame];
    [buttonLearn addTarget:self action:@selector(learnMorePressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonLearn];
}

-(void)viewWillAppear:(BOOL)animated {
    
    [self.navigationController.view setNeedsLayout];
    [self.tabBarController.view setNeedsLayout];
}

- (IBAction)pressedLogout:(UIButton *)sender {

    [[NSUserDefaults standardUserDefaults] setValue:@"" forKey:kKeyUserId];
    [self.tabBarController performSegueWithIdentifier:@"SegueToLogin" sender:self];
}

- (IBAction)pressedFeedback:(UIButton *)sender {

    MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
    mailComposer.mailComposeDelegate = self;
    [mailComposer setSubject:@"Gekko feedback"];
    [mailComposer setToRecipients:[NSArray arrayWithObject:@"gekko@gmail.com"]];
    [self presentViewController:mailComposer animated:YES completion:nil];
}

- (IBAction)backPressed:(UIButton *)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)pressedClear:(UIButton *)sender {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"CLEAR DATA", nil) message:NSLocalizedString(@"CLEAR DATA MESSAGE", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"NO", nil) otherButtonTitles:NSLocalizedString(@"YES", nil), nil];
    [alert show];

}

- (IBAction)pressedRecomend:(UIButton *)sender {
    
    [self performSegueWithIdentifier:@"PushToRecomend" sender:self];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:NSLocalizedString(@"YES", nil)]) {
        
        [ArchiverUnarchiver clearData];
        [[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:kKeyShouldClear];
        [[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:kKeyShoudReload];
    }
}

- (void)learnMorePressed:(UIButton *)sender {
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:kURLGetGekko]];
}


- (void)viewDidUnload {
    [self setButtonRecomend:nil];
    [self setLabelSlogan:nil];
    [super viewDidUnload];
}
@end
